package Test1;

import Final.Sql;

import javax.swing.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SelectByName {

    static JLabel labID = new JLabel();
    static JTextField textID = new JTextField();
    static JFrame jFrame = new JFrame("结果窗口");
    static ResultSet rs;
    //static TextField textField = new TextField();
    static String disp = "";
    static JTable jTable = new JTable();
    public SelectByName() {
        String[] columnNames={"ID","名字","性别","籍贯","班级","年龄"}; //列名

        Object[][] rowData=new Object[6][6]; //表格数据
        JFrame jf = new JFrame();
        jf.setTitle("查询界面");
        jf.setSize(340, 200);
        //jf.setDefaultCloseOperation(3);
        jf.setLayout(new FlowLayout());
        jf.setLocationRelativeTo(null);
        jf.setResizable(false);

        java.awt.FlowLayout fl = new java.awt.FlowLayout(FlowLayout.CENTER, 5, 5);
        jf.setLayout(fl);

        java.awt.Dimension dim2 = new java.awt.Dimension(50, 20);//标签的大小
        java.awt.Dimension dim3 = new java.awt.Dimension(250, 30);//输入框的大小
        java.awt.Dimension dim4 = new java.awt.Dimension(70, 40);//按钮的大小


        //添加说明标签
        JLabel jLabel = new JLabel("请输入");
        jLabel.setPreferredSize(dim2);
        jf.add(jLabel);

        //添加“学号”标签

        labID.setText("名字");
        labID.setPreferredSize(dim2);
        jf.add(labID);

        //添加账号输入框，并添加监控事件

        textID.setPreferredSize(dim3);
        jf.add(textID);

        //添加一个删除按钮
        JButton button_delete = new JButton();
        button_delete.setText("查询");
        button_delete.setPreferredSize(dim4);
        button_delete.setBounds(10, 300, 300, 40);
        jf.add(button_delete);

        //添加取消按钮
        JButton button_cancle = new JButton();
        button_cancle.setText("取消");
        button_cancle.setPreferredSize(dim4);
        button_cancle.setBounds(10, 300, 300, 40);
        jf.add(button_cancle);

        jf.setVisible(true);

        button_delete.addActionListener(e -> {
            Connection conn = Sql.open();




            try {

                String sql = "select * from Student where name=?";

                PreparedStatement pstmt = null;
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,textID.getText());
                rs = pstmt.executeQuery();
                //rs.next();
                int count = 0;
                int flag = 0;
                while(rs.next())
                {
                    flag = 1;
                    rowData[count][0]=rs.getString("ID"); //初始化数组内容
                    rowData[count][1]=rs.getString("name");
                    rowData[count][2]=rs.getString("sex");
                    rowData[count][3]=rs.getString("Origin");
                    rowData[count][4]=rs.getString("Class");
                    rowData[count][5]=Integer.toString(rs.getInt("age"));

                    count++;

                }
                if(flag == 1){
                    System.out.println(disp);
                    //textField.setText(disp);
                    jLabel.setText(disp);
                    jFrame.setSize(1000, 200);
                    jTable=new JTable(rowData,columnNames); //实例化表格

                    jTable.getColumn("年龄").setMaxWidth(5); //设置行宽
                    jFrame.add(new JScrollPane(jTable),BorderLayout.CENTER); //增加组件
                    jFrame.setVisible(true);
                }else {
                    new NoFond();
                }


                //Student s = new Student(ID,name,sex,Origin,Class,age);

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                Sql.close(conn);
            }

        });

        button_cancle.addActionListener(e -> {

            jf.dispose();

        });



    }

     class NoFond{
        public NoFond(){
            JFrame jFrame = new JFrame();
            jFrame.setTitle("提示界面");
            jFrame.setSize(340, 100);
            jFrame.setLayout(new FlowLayout());
            jFrame.setLocationRelativeTo(null);
            jFrame.setResizable(false);

            JLabel jLabel = new JLabel();
            jLabel.setText("查无此人！");

            JButton jButton = new JButton();
            jButton.setText("返回");

            jFrame.add(jLabel);
            jFrame.add(jButton);
            jFrame.setVisible(true);

            jButton.addActionListener(e -> {

                jFrame.dispose();

            });
        }
    }
}
